/*
 * Copyright 2013 HVTTGROUP
 * Tool Genneric code by hvttgroup
 * @version v1.0
 * @see http://hvttgroup.com
 *
 */
package com.hvtt.web.models;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.utilities.Utilities;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 *
 * @author Nguyen Hoang Danh
 */
public class User implements Serializable{

// <editor-fold defaultstate="collapsed" desc="Generic code properties for class">
    private String userID = "";
    private String userGroupID = "";
    private String password = "";
    private String firstName = "";
    private String lastName = "";
    private String fullName = "";
    private String email = "";
    private String numPhone = "";
    private boolean active = false;
    private int status = 0;
    private String description = "";
    private WEBConnection webConnection;
    private static String STORE_NAME = "{call proc_user(?,?,?,?,?,?,?,?,?,?,?,?)}";
    private static String TABLE_NAME = "user";
    private static final String key = "UserID";
    private static final String key1 = "UserGroupID";

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code Constructor class">
    public User() {
        webConnection = WEBConnection.getInstance();
    }

    public User(String userID, String userGroupID, String password, String firstName, String lastName, String fullName, String email, String numPhone, boolean active, int status, String description) {
        this.userID = userID;
        this.userGroupID = userGroupID;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = fullName;
        this.email = email;
        this.numPhone = numPhone;
        this.active = active;
        this.status = status;
        this.description = description;
        webConnection = WEBConnection.getInstance();
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code init param">
    private WEBParamCollection fillParamsAndAction(String action) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(this.getUserID());
        params.add(this.getUserGroupID());
        params.add(this.getPassword());
        params.add(this.getFirstName());
        params.add(this.getLastName());
        params.add(this.getFullName());
        params.add(this.getEmail());
        params.add(this.getNumPhone());
        params.add(this.getActive());
        params.add(this.getStatus());
        params.add(this.getDescription());
        return params;
    }

    private WEBParamCollection fillParamsAndAction(String action, User user1) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(user1.getUserID());
        params.add(user1.getUserGroupID());
        params.add(user1.getPassword());
        params.add(user1.getFirstName());
        params.add(user1.getLastName());
        params.add(user1.getFullName());
        params.add(user1.getEmail());
        params.add(user1.getNumPhone());
        params.add(user1.getActive());
        params.add(user1.getStatus());
        params.add(user1.getDescription());
        return params;
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code event">
    public boolean insert(User varUser) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add", varUser));
        return bExcute;
    }

    public boolean save() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add"));
        return bExcute;
    }

    public boolean update(User varUser) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update", varUser));
        return bExcute;
    }

    public boolean update() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update"));
        return bExcute;
    }

    public boolean remove(String key) {
        this.setUserID(key);
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Delete"));
        return bExcute;
    }

    public boolean remove(List<User> listUser) {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean result = false;
        String sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
        for (int i = 0; i < listUser.size(); i++) {
            sql = sql + "?,";
            p.add(listUser.get(i).getUserID());
            if (i % 900 == 0) {
                sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
                result = webConnection.executeUpdate(sql, Command.sqlText, p);
                p.clear();
                sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
            }
        }
        if (sql.lastIndexOf(",") != -1) {
            sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
            result = webConnection.executeUpdate(sql, Command.sqlText, p);
        }
        return result;
    }

    public boolean removeAll() {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean bExcute = webConnection.executeUpdate("delete from " + TABLE_NAME, Command.sqlText, p);
        return bExcute;
    }

    public User findByKey(String key) {
        User varUser1 = null;
        try {
            this.setUserID(key);

            List<User> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetByKey"), User.class);
            if (!result.isEmpty()) {
                varUser1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varUser1;
    }

    public User checkLogin(String userID, String password) {
        User varUser1 = null;
        try {
            this.setUserID(userID);
            this.setPassword(Utilities.encrypt(password));
            List<User> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("CheckLogin"), User.class);
            if (!result.isEmpty()) {
                varUser1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varUser1;
    }

    public List<User> findAll() {
        List<User> lstUser = null;
        try {
            lstUser = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetAll"), User.class);
        } catch (SQLException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return lstUser == null ? new ArrayList<User>() : lstUser;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code getter and setter for properties of class">
    public String getUserID() {
        return userID;
    }

    public String getUserGroupID() {
        return userGroupID;
    }

    public String getPassword() {
        return password;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFullName() {
        return fullName;
    }

    public String getEmail() {
        return email;
    }

    public String getNumPhone() {
        return numPhone;
    }

    public boolean getActive() {
        return active;
    }

    public int getStatus() {
        return status;
    }

    public String getDescription() {
        return description;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public void setUserGroupID(String userGroupID) {
        this.userGroupID = userGroupID;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setNumPhone(String numPhone) {
        this.numPhone = numPhone;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void resetAttribute() {
        userID = "";
        userGroupID = "";
        password = "";
        firstName = "";
        lastName = "";
        fullName = "";
        email = "";
        numPhone = "";
        active = false;
        status = 0;
        description = "";
    }

    public void setWebConnection(WEBConnection webConnection) {
        this.webConnection = webConnection;
    }

    public WEBConnection getWebConnection() {
        return webConnection;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code hasCode and equals method">
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.userID).append(this.userGroupID).append(this.password).append(this.firstName).append(this.lastName).append(this.fullName).append(this.email).append(this.numPhone).append(this.active).append(this.status).append(this.description).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof User) {
            User otherObj = (User) obj;
            return new EqualsBuilder().append(this.userID, otherObj.userID).append(this.userGroupID, otherObj.userGroupID).append(this.password, otherObj.password).append(this.firstName, otherObj.firstName).append(this.lastName, otherObj.lastName).append(this.fullName, otherObj.fullName).append(this.email, otherObj.email).append(this.numPhone, otherObj.numPhone).append(this.active, otherObj.active).append(this.status, otherObj.status).append(this.description, otherObj.description).isEquals();
        }
        return false;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code filterFields">
    public static Field[] filterFields() {
        try {
            Class c = User.class;
            return new Field[]{c.getDeclaredField("userID"), c.getDeclaredField("userGroupID"), c.getDeclaredField("fullName"), c.getDeclaredField("email"), c.getDeclaredField("numPhone"), c.getDeclaredField("description")};
        } catch (NoSuchFieldException | SecurityException ex) {
            Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Field[]{};
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code other method">
    public User newInstance() {
        try {
            return this.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return new User();
    }

    /**
     * Copy properties form User to this
     *
     * @param user1
     * @return User
     */
    public User newInstance(User user1) {
        this.setUserID(user1.getUserID());
        this.setUserGroupID(user1.getUserGroupID());
        this.setPassword(user1.getPassword());
        this.setFirstName(user1.getFirstName());
        this.setLastName(user1.getLastName());
        this.setFullName(user1.getFullName());
        this.setEmail(user1.getEmail());
        this.setNumPhone(user1.getNumPhone());
        this.setActive(user1.getActive());
        this.setStatus(user1.getStatus());
        this.setDescription(user1.getDescription());
        return this;
    }

    //</editor-fold>
    @Override
    public String toString() {
        return "User :{ " + userID + " , " + password + " }"; //To change body of generated methods, choose Tools | Templates.
    }
}
